using System;

///<author>Seth Peck</author>
///<email>seth@poetic.com</email>
///<date>12/13/2001</date>
namespace BTVPodcatcherNamespace
{
	/// <summary>
	/// StringFilter was originally created so Devhood could remove HTML tags from member names, 
	/// but it has turned into something a little bigger.  The various functions can also be used
	/// to remove angle brackets, parentheses, braces, brackets, comments and expletives (curses turn into !@#$*).
	/// 
	/// If an error occurs in any of these functions, it is because an error in the coding (such as nonsense ordering
	/// of angle brackets) existed, and for validation purposes these functions will return an empty string (if set to false)
	/// or the original string (if set to true).
	/// </summary>
	public class StringFilter
	{
		/// <summary>
		/// Filters a string from another string. 
		/// </summary>
		/// <param name="keepIfError">If this is set to true, and an error occurs, the method
		/// returns the original string.  Otherwise, it returns an empty string.</param>
		/// <param name="mystring">The string to be parsed.</param>
		/// <param name="s">A string (character, word or words)</param>
		/// <returns>The string, with the string s replaced by random nonsense characters.</returns>
		public static string ExplicitFilter(bool keepIfError, string mystring, string s)
		{
			string oldstring = mystring;
			string start, end;
			bool explicit_filter = false;
			switch (s)
			{
				case "<": case ">":
					start = "<";
					end = ">";
					break;
				case "{": case "}":
					start = "{";
					end = "}";
					break;
				case "[": case "]":
					start = "[";
					end = "]";
					break;
				case "(": case ")":
					start = "(";
					end = ")";
					break;
				case "/*": case "*/":
					start = "/*";
					end = "*/";
					break;
				case "//":
					start = "//";
					end = "\n";
					break;
				case "///":
					start = "///";
					end = "\n";
					break;
				default:
					explicit_filter = true;
					start = "nothing";
					end = "nothing";
					break;
			}
			if (explicit_filter)
			{
				string newword = "";
				string curses = "!@#$%^&*";
				Random random = new Random();
				for (int i = 0; i < s.Length; i++)
					newword += curses[random.Next(curses.Length)];

				string lowerword = s.ToLower();
				string lowerstring = mystring.ToLower();
				while (lowerstring.IndexOf(lowerword) > -1)
				{
					mystring = mystring.Substring(0,lowerstring.IndexOf(lowerword)) + newword + mystring.Substring(lowerstring.IndexOf(lowerword) + lowerword.Length);
					lowerstring = lowerstring.Replace(lowerword, newword);
				}
				return mystring;
			}
			else
			{
				try
				{
					while (mystring.IndexOf(start) != -1)
					{
						int startRemove = mystring.IndexOf(start);
						mystring = mystring.Remove(startRemove,mystring.IndexOf(end)-startRemove + end.Length);
						if (mystring.IndexOf(end) < mystring.IndexOf(start))
							mystring = mystring.Remove(startRemove, mystring.IndexOf(end) - end.Length);
					}
				}
				catch (Exception)
				{
					if (keepIfError)
						return oldstring;
					else
						return "";
				}
				return mystring;
			}
		}

		/// <summary>
		/// Filters a list of strings from another string. 
		/// </summary>
		/// <param name="keepIfError">If this is set to true, and an error occurs, the method
		/// returns the original string.  Otherwise, it returns an empty string.</param>
		/// <param name="mystring">The string to be parsed.</param>
		/// <param name="s">Any number of strings to be filtered out.</param>
		/// <returns>The string, with the string s replaced by random nonsense characters.</returns>
		public static string ExplicitFilter(bool keepIfError, string mystring, params string[] s)
		{
			foreach (string z in s)
				mystring = ExplicitFilter(keepIfError,mystring,z);
			return mystring;
		}

		/// <summary>
		/// Filters all comments of type ///, // or /**/ from the string. 
		/// </summary>
		/// <param name="keepIfError">If this is set to true, and an error occurs, the method
		/// returns the original string.  Otherwise, it returns an empty string.</param>
		/// <param name="mystring">The string to be parsed.</param>
		/// <returns>The string, without the comments.</returns>
		public static string FilterComments(bool keepIfError, string mystring)
		{
			mystring = ExplicitFilter(keepIfError,mystring,"//","/*","///");
			return mystring;
		}


		/// <summary>
		/// Filters all angle bracket tags (a la HTML, XML or PHP) from the string.
		/// </summary>
		/// <param name="keepIfError">If this is set to true, and an error occurs, the method
		/// returns the original string.  Otherwise, it returns an empty string.</param>
		/// <param name="mystring">The string to be parsed.</param>
		/// <returns>The string, without the markup tags.</returns>
		public static string FilterMarkupTags(bool keepIfError, string mystring)
		{
			mystring = ExplicitFilter(keepIfError,mystring,"<");
			return mystring;
		}

		/// <summary>
		/// Filters all braces and the content within [this stuff] from the string.
		/// </summary>
		/// <param name="keepIfError">If this is set to true, and an error occurs, the method
		/// returns the original string.  Otherwise, it returns an empty string.</param>
		/// <param name="mystring">The string to be parsed.</param>
		/// <returns>The string, without the braced text.</returns>
		public static string FilterBraces(bool keepIfError, string mystring)
		{
			mystring = ExplicitFilter(keepIfError,mystring,"[");
			return mystring;
		}

		/// <summary>
		/// Filters all brackets and the content within {this stuff} from the string.
		/// </summary>
		/// <param name="keepIfError">If this is set to true, and an error occurs, the method
		/// returns the original string.  Otherwise, it returns an empty string.</param>
		/// <param name="mystring">The string to be parsed.</param>
		/// <returns>The string, without the bracketed text.</returns>
		public static string FilterBrackets(bool keepIfError, string mystring)
		{
			mystring = ExplicitFilter(keepIfError,mystring,"{");
			return mystring;
		}

		/// <summary>
		/// Filters all angle parenthetical comments (this stuff) from the string.
		/// </summary>
		/// <param name="keepIfError">If this is set to true, and an error occurs, the method
		/// returns the original string.  Otherwise, it returns an empty string.</param>
		/// <param name="mystring">The string to be parsed.</param>
		/// <returns>The string, without the parenthetical comments.</returns>
		public static string FilterParentheses(bool keepIfError, string mystring)
		{
			mystring = ExplicitFilter(keepIfError,mystring,"(");
			return mystring;
		}

		/// <summary>
		/// Replaces all occurrences of oldValue with newValue, ignoring the case of oldValue.
		/// </summary>
		/// <param name="mystring">The string to be parsed for replacement.</param>
		/// <param name="oldValue">A string to look for.  Case for this parameter does not matter.</param>
		/// <param name="newValue">A string to put in place.  Case for this parameter matters.</param>
		/// <returns>A string with the replaced words.</returns>
		public static string ReplaceIgnoreCase(string mystring, string oldValue, string newValue)
		{
			string lowerword = oldValue.ToLower();
			string lowerstring = mystring.ToLower();
			while (lowerstring.IndexOf(lowerword) > -1)
			{
				mystring = mystring.Substring(0,lowerstring.IndexOf(lowerword)) + newValue + mystring.Substring(lowerstring.IndexOf(lowerword) + lowerword.Length);
				lowerstring = lowerstring.Substring(0,lowerstring.IndexOf(lowerword)) + newValue + lowerstring.Substring(lowerstring.IndexOf(lowerword)+lowerword.Length);
			}
			return mystring;
		}

		/*public static void Main()
		{
			string s = "The quick brown FOX jumps over the lazy dog.  The Fox is a dumb fOx indeed";
			Console.WriteLine(s);
			string t = ReplaceIgnoreCase(s, "FOX", "wolf");
			Console.WriteLine(t);
			Console.ReadLine();
		}*/
	}
}

